package main.leetcode.clockin.September;

import java.util.ArrayList;
import java.util.List;

/**
 * 78. 子集 给定一组不含重复元素的整数数组 nums，返回该数组所有可能的子集（幂集）。
 *
 * <p>说明：解集不能包含重复的子集。
 *
 * <p>示例:<br>
 * 输入: nums = [1,2,3] <br>
 * 输出: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]
 */
public class day20 {
    private List<List<Integer>> res;

    public static void main(String[] args) {
        System.out.println(new day20().subsets(new int[] {1, 2, 3}));
    }

    /** 回溯 */
    public List<List<Integer>> subsets(int[] nums) {
        res = new ArrayList<>();
        if (nums == null || nums.length == 0) {
            return res;
        }
        int n = nums.length;
        for (int i = 0; i <= n; ++i) {
            dfs(nums, new ArrayList<>(), 0, i);
        }
        return res;
    }

    private void dfs(int[] nums, List<Integer> cur, int start, int remaining) {
        if (remaining == 0) {
            res.add(new ArrayList<>(cur));
            return;
        }

        int n = nums.length;
        for (int i = start; i < n; ++i) {
            cur.add(nums[i]);
            dfs(nums, cur, i + 1, remaining - 1);
            cur.remove(cur.size() - 1);
        }
    }
}
